package org.algorithm.biubiu.wheel_1.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;
import org.junit.Test;


/**
 * @Date 2022/2/11 16:49
 * @Created by biubiu
 * @Link {https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/}
 */
public class 翻转链表 {


    @Test
    public void test(){
        ListNode build = ListNode.build(1, 2, 3, 4,5,6,7,8,9);
        System.out.println(ListNode.print(build));
        ListNode listNode = reverseList(build);
        String print = ListNode.print(listNode);
        System.out.println(print);
    }

    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode left = head;
        ListNode right = left.next;
        left.next = null;
        while (right != null) {
            ListNode next = right.next;
            right.next = left;
            left = right;
            right = next;
        }
        return left;
    }

}
